log using "./analysis_log", text replace

/*********************************************************
Title: Mind the Gap: Why Wealthy Voters Support Brexit
Authors: Jane Green and Raluca L Pahontu
**********************************************************/

/*********************************************************

						Table_of_contents
						
* 1. Set Directory
* 2. Data Cleaning
* 3. Data Descriptives
* 4. Data Preparation
* 5. Data Analysis
**********************************************************/

clear all
set maxvar 10000
set scheme plotplainblind
set more off

************************************************************************
* 							1. Set Directory
************************************************************************

global main "~/Dropbox/Replication/BJPS/"
global code "~/Dropbox/Replication/BJPS/code"
global input "~/Dropbox/Replication/BJPS/input"
global temp "~/Dropbox/Replication/BJPS/temp"
global output "~/Dropbox/Replication/BJPS/figures"

cd "$main"

cd "~/Dropbox/Replication/BJPS/"

************************************************************************
* 							2. Data Cleaning
************************************************************************

do "$code/2-dataclean"
besw14b


global cfe gender ageW14 i.anyUniW14 al_scaleW14 i.married i.wkstat i.onsAreaSupergroupW14

boe

global cfep i.regionid i.year
global cfef  c.age##c.age i.educ i.wkstat i.regionid i.year


************************************************************************
* 							3. Data Descriptives
************************************************************************


*************
* Table A1
*************
preserve
besw14b
svyset [pw=wt_new_W14] 

gen homevalue = 0 if homeOwnW14 == 3 | homeOwnW14 == 4 | homeOwnW14 == 5 // no asset is renting
replace homevalue = 24999.5 if homeAmtbW14 ==  1
replace homevalue = 74999.5 if homeAmtbW14 ==  2
replace homevalue = 124999.5 if homeAmtbW14 ==  3
replace homevalue = 174999.5 if homeAmtbW14 ==  4
replace homevalue = 224999.5 if homeAmtbW14 ==  5
replace homevalue = 274999.5 if homeAmtbW14 ==  6
replace homevalue = 349999.5 if homeAmtbW14 ==  7
replace homevalue = 449999.5 if homeAmtbW14 ==   8
replace homevalue = 549999.5 if homeAmtbW14 ==  9
replace homevalue = 649999.5 if homeAmtbW14 == 10
replace homevalue = 749999.5 if homeAmtbW14 == 11
replace homevalue = 849999.5 if homeAmtbW14 == 12
replace homevalue = 949999.5 if homeAmtbW14 == 13

svy: tab wkstat
svy: tab homeOwnW14
svy: mean homevalue if homevalue>0
drop homevalue
restore

*************
* Figure A1
*************

preserve 
besw14b
svyset [pw=wt_new_W14] 

*Income
qui svy: tab profile_gross_householdW14
gen incomereport = 1 if profile_gross_householdW14 >=1 & profile_gross_householdW14<16
replace incomereport = 0 if profile_gross_householdW14==17 | profile_gross_householdW14 == 9999 

*Debt
qui svy: tab debtW14
gen debtreport = 1 if debtW14 == 0 | debtW14 == 1
replace debtreport = 0 if debtW14 == 9999 

qui svy: tab debtAmtbW14 
gen debtamtreport = 1 if debtAmtbW14 >=1 & debtAmtbW14<=17
replace debtamtreport = 0 if debtAmtbW14 ==9999 

*Savings
qui svy: tab savingsW14
gen savereport = 1 if savingsW14 == 0 | savingsW14 == 1 
replace savereport = 0 if savingsW14 == 9999
 
qui svy: tab savingsAmtbW14
gen saveamtreport = 1 if savingsAmtbW14 >=1 &  savingsAmtbW14 <= 17
replace saveamtreport = 0 if savingsAmtbW14 == 9999
 
* Rent
qui svy: tab rentPaymentW14
gen rentreport = 1 if rentPaymentW14 >=0 & rentPaymentW14<= 100 
replace rentreport = 0 if rentPaymentW14 == 9999

*Mortgage
qui svy: tab mortgagePaymentW14
gen mortgagereport = 1 if mortgagePaymentW14 >=0 & mortgagePaymentW14 <= 100
replace mortgagereport = 0 if mortgagePaymentW14 == 9999

*Home Value
qui svy: tab homeOwnW14
gen homeownreport = 1 if homeOwnW14 >= 1 & homeOwnW14 <=5
replace homeownreport = 0 if homeOwnW14 == 9999

qui svy: tab homeAmtb
gen homeamtreport = 1 if homeAmtb >= 1 & homeAmtb <=14
replace homeamtreport = 0 if homeAmtb == 9999 

qui reg  incomereport leave $cfe, r 
estimates store l11		
qui reg  debtreport leave $cfe, r
estimates store l21
qui reg  debtamtreport leave $cfe, r
estimates store l31
qui reg  savereport leave $cfe, r
estimates store l41
qui reg  saveamtreport leave $cfe, r
estimates store l51
qui reg  rentreport leave $cfe, r
estimates store l61
qui reg  mortgagereport leave $cfe, r
estimates store l71
qui reg  homeownreport leave $cfe, r
estimates store l81
qui reg  homeamtreport leave $cfe, r
estimates store l91

coefplot l11 l21 l31 l41 l51 l61 l71 l81 l91 /// //l101 l111 
, keep (leave) xline(0, lcolor(red) lstyle(solid)) msymbol(circle)   legend(off)  aseq swapnames xlabel(-.15 -.1 -.05 0 .05 .1 .15)  ///
    coeflabels(l11 = "Income Reporting" ///
			   l21 = "Debt Reporting"  ///
               l31 = "Debt Amount Reporting" /// 
			   l41 = "Savings  Reporting" /// 
               l51 = "Savings Amount  Reporting" /// 
               l61 = "Rent Amount  Reporting" /// 
               l71 = "Mortgage Amount  Reporting" /// 
               l81 = "Home Ownership  Reporting" /// 
               l91 = "Home Amount  Reporting" ) mfcolor(white) mlcolor (black) msize(1.5) ciopts(lpatt(solid)lcol(black)) 
			  
graph export "$output/fig_a1bes.pdf", replace
restore


preserve
boe
svyset [pw=weight] 

replace hsval_m = "." if hsval_m == "don't know"
replace hsval_m = "." if hsval_m == "refused"
replace hsval_m = "." if hsval_m == "NA"
replace hsval_m = "." if hsval_m == "Don't know"
replace hsval_m = "." if hsval_m == "not applicable"
replace hsval_m = "." if hsval_m == "Not applicable"
replace hsval_m = "." if hsval_m == "Refused"
destring hsval_m, replace

rename hsval_m homevalue

svy: tab tenure
svy: tab wkstat
svy: mean homevalue if homevalue >0
restore

preserve 
boe

gen incomereport = 1 if fihhyr2_m !=. 
replace incomereport = 0 if fihhyr2_m == .

replace nvesttot_m = "." if nvesttot_m == "refused"
replace nvesttot_m = "." if nvesttot_m == "NA"
replace nvesttot_m = "." if nvesttot_m == "Not applicable"
replace nvesttot_m = "." if nvesttot_m == "not applicable"

destring nvesttot_m, replace 
rename nvesttot_m totalsaving

gen savereport = 1 if totalsaving !=. 
replace savereport = 0 if totalsaving == .


replace ustot_m = "." if ustot_m == "don't know"
replace ustot_m = "." if ustot_m == "refused"
replace ustot_m = "." if ustot_m == "NA"
replace ustot_m = "." if ustot_m == "Don't know"
replace ustot_m = "." if ustot_m == "Not applicable"
replace ustot_m = "." if ustot_m == "not applicable"
replace ustot_m = "." if ustot_m == "Refused"
destring ustot_m, replace
rename ustot_m debtamount
gen totaldebt = debtamount
replace totaldebt = 0 if us == "0" | us == "No"

gen debtreport = 1 if totaldebt !=. 
replace debtreport = 0 if totaldebt == .

replace mrent = "." if mrent == "Don't know"
replace mrent = "." if mrent == "NA"
replace mrent = "." if mrent == "Prefer not to state"
destring mrent, replace
gen rent_y = 12 * mrent

gen owner = 1 if tenure == "Owned mortgage" | tenure == "Owned outright"
replace owner = 0 if tenure == "Housing association rented" | tenure == "Local authority rented" | tenure == "Private rented"

replace rent_y = 0 if owner == 1 

gen rentreport = 1 if rent_y !=. 
replace rentreport = 0 if rent_y == .

replace xpmg_m = "." if xpmg_m == "don't know"
replace xpmg_m = "." if xpmg_m == "refused"
replace xpmg_m = "." if xpmg_m == "NA"
replace xpmg_m = "." if xpmg_m == "Don't know"
replace xpmg_m = "." if xpmg_m == "Not applicable"
replace xpmg_m = "." if xpmg_m == "not applicable"
replace xpmg_m = "." if xpmg_m == "Refused"
destring xpmg_m, replace
rename xpmg_m mortgage_m
gen mortgage_y = 12 * mortgage_m 

replace mortgage_y = 0 if tenure == "Owned outright" | tenure == "Housing association rented" | tenure == "Local authority rented" | tenure == "Private rented" 

gen mortgagereport = 1 if mortgage_y !=. 
replace mortgagereport = 0 if mortgage_y == .

gen homereport = 1 if owner !=. 
replace homereport = 0 if owner == .

replace hsval_m = "." if hsval_m == "don't know"
replace hsval_m = "." if hsval_m == "refused"
replace hsval_m = "." if hsval_m == "NA"
replace hsval_m = "." if hsval_m == "Don't know"
replace hsval_m = "." if hsval_m == "not applicable"
replace hsval_m = "." if hsval_m == "Not applicable"
replace hsval_m = "." if hsval_m == "Refused"
destring hsval_m, replace

rename hsval_m homevalue
replace homevalue = 0 if tenure == "Housing association rented" | tenure == "Local authority rented" | tenure == "Private rented"

gen homeAMTreport = 1 if homevalue !=. 
replace homeAMTreport = 0 if homevalue == .

global controls female c.age##c.age i.educ i.wkstat i.regionid 

qui reg incomereport leave $controls, cl(subsid)
estimate store incomereport
qui reg savereport leave $controls, cl(subsid)
estimate store savereport
qui reg debtreport leave $controls, cl(subsid)
estimate store debtreport
qui reg rentreport leave $controls, cl(subsid)
estimate store rentreport
qui reg mortgagereport leave $controls, cl(subsid)
estimate store mortgagereport
qui reg homereport leave $controls, cl(subsid)
estimate store homereport
qui reg homeAMTreport leave $controls, cl(subsid)
estimate store homeAMTreport

coefplot incomereport savereport debtreport rentreport mortgagereport homereport homeAMTreport, keep (leave) xline(0, lcolor(red) lstyle(solid)) aseq swapnames ///
legend(off) mfcolor(white) mlcolor (black) ciopts(lpatt(solid)lcol(black)) msymbol(circle) ///
 coeflabels(incomereport = "Income Reporting" ///
			   debtreport = "Debt Reporting"  ///
			   savereport = "Savings  Reporting" /// 
               rentreport = "Rent Amount  Reporting" /// 
               mortgagereport = "Mortgage Amount  Reporting" /// 
               homereport = "Home Ownership  Reporting" /// 
               homeAMTreport = "Home Amount  Reporting" )  msize(1.5)  xlabel(-.04 -.02 0 0.02 .04)
			  
graph export "$output/fig_a1boe.pdf", replace
restore


************************************************************************
* 							4. Data Preparation
************************************************************************

do "$code/3-wealthgenerate"

preserve 
keep EUIntegrationSelfW8 euRefVoteW9
replace EUIntegrationSelfW8 = . if EUIntegrationSelfW8 == 9999
gen leaveW9 = 1 if euRefVoteW9 == 1
replace leaveW9 = 0 if euRefVoteW9 == 0
save "$temp/fig1data", replace
restore

preserve

gen nochangeECON = 1 if effectsEUEconW14 == 3 
replace nochangeECON = 0 if effectsEUEconW14 == 1 | effectsEUEconW14 == 2 | effectsEUEconW14 == 4 |effectsEUEconW14 ==5

gen nochangePERSONAL =1 if effectsEUFinanceW14 == 3 
replace nochangePERSONAL = 0 if effectsEUFinanceW14 == 1 | effectsEUFinanceW14 == 2 | effectsEUFinanceW14 ==4 | effectsEUFinanceW14 ==5


qui reg nochangeECON c.z_p_wealth z_income_disHH $cfe, r 
qui margins, at(z_p_wealth = (-1 0 1 2)) post
regsave  using "$temp/fig3data",  replace   ci

qui reg nochangePERSONAL c.z_p_wealth z_income_disHH $cfe, r 
qui margins, at(z_p_wealth = (-1 0 1 2)) post
regsave  using "$temp/fig3data",  append   ci
restore
 
preserve

replace riskUnemploymentW14 = . if riskUnemploymentW14 == 9999

qui reg leave z_income_disHH z_f_wealth z_p_wealth $cfe riskUnemploymentW14,  r
regsave using "$temp/figd2data", replace ci

replace EUIntegrationSelfW14 = . if EUIntegrationSelfW14 == 9999
qui reg leave z_income_disHH z_f_wealth z_p_wealth $cfe EUIntegrationSelfW14, r
regsave using "$temp/figd2data", append ci

use "$temp/figd2data", clear
keep if var == "z_f_wealth" | var == "z_p_wealth"
save "$temp/figd2data_toplot", replace

rm "$temp/figd2data.dta"
restore 

preserve

gen betterchangeECON = 1 if effectsEUEconW14 == 4 | effectsEUEconW14 == 5
replace betterchangeECON = 0 if effectsEUEconW14 == 1 | effectsEUEconW14 == 2 

gen betterchangePERSONAL =1 if effectsEUFinanceW14 == 4 | effectsEUFinanceW14 ==5 
replace betterchangePERSONAL = 0 if effectsEUFinanceW14 == 1 | effectsEUFinanceW14 == 2 

qui reg betterchangeECON c.z_p_wealth z_income_disHH $cfe, r 
regsave  using "$temp/figd3data",  replace   ci

qui reg betterchangePERSONAL c.z_p_wealth z_income_disHH $cfe, r 
regsave  using "$temp/figd3data",  append   ci

restore
 

preserve
program define b
boe
do "$code/3-wealthgenerateboe"
end
restore
 
program define yougov
use "$input/yougov.dta", clear

gen treat = 1 if randgroup == 3
replace treat = 0 if randgroup == 5

replace EUrefsatisfactionExp_1W19 = . if EUrefsatisfactionExp_1W19 == 12
replace EUrefsatisfactionControl_1W19 = . if EUrefsatisfactionControl_1W19 == 12

gen outcome = EUrefsatisfactionExp_1W19 if treat == 1
replace outcome = EUrefsatisfactionControl_1W19 if treat == 0

qui tab  anyUniW19 
replace  anyUniW19  = . if  anyUniW19  == 9999

drop if anyUniW19 == . 

qui tab workingStatusW19

gen wkstat = 1 if workingStatusW19 == 1 |  workingStatusW19 == 2 | workingStatusW19 ==3 
replace wkstat = 2 if workingStatusW19 == 4 // unemployed
replace wkstat = 3 if workingStatusW19 == 5 | workingStatusW19 == 6  // student
replace wkstat = 4 if workingStatusW19 == 7 // retired 
replace wkstat = 5 if workingStatusW19 == 8 // not in paid work for any other reason
drop if wkstat == . 

gen female = 1 if gender == 2
replace female = 0 if gender == 1
 
global cfexp i.anyUniW19 i.wkstat female  ageW19

gen risk = 0 if expRiskIncomeW19 == 2 & expRiskIncomeaW19 == 2 
replace risk = 1 if expRiskIncomeW19 == 2 & expRiskIncomeaW19 == 1
replace risk = 2 if expRiskIncomeW19 == 1 & expRiskIncomebW19 == 2
replace risk = 3 if expRiskIncomeW19 == 1 & expRiskIncomebW19 == 1 

replace risk = 0 if controlRiskIncomeW19 == 2 & controlRiskIncomeaW19 == 2
replace risk = 1 if controlRiskIncomeW19 == 2 & controlRiskIncomeaW19 == 1 
replace risk = 2 if controlRiskIncomeW19 == 1 & controlRiskIncomebW19 == 2 
replace risk = 3 if controlRiskIncomeW19 == 1 & controlRiskIncomebW19 == 1 


gen risktreat = 1 if randgroup  == 4
replace risktreat = 0 if randgroup  == 6
end

*************
* Figure B4
*************

preserve 
b

gen poor2 = 1 if z_income_disHH <=0 & z_income_disHH <.
replace  poor2= 0 if z_income_disHH >0 & z_income_disHH <.


catplot poor2 tenure, ///
percent(poor2) ///
var1opts(label(labsize(small))) ///
var2opts(label(labsize(small)) relabel(`r(relabel)')) ///
ytitle("Percent of Respondents by Richness ", size(small)) ///
blabel(bar, format(%4.1f)) ///
intensity(25) ///
asyvars legend(on order(1 "High" 2 "Low"))
graph export "$output/fig_b4.pdf",  replace
restore

*************
* Figure B5
*************

preserve
b
qui sum totalsaving
gen meanS =  r(mean)

gen lowsave = 1 if totalsaving >=0 & totalsaving <= meanS
replace lowsave = 0 if totalsaving >meanS & totalsaving !=.

twoway (kdensity z_income_disHH if lowsave == 0 , color(orange)  )  ///
 (kdensity z_income_disHH if lowsave == 1, lpattern(solid) color(black) ), ///
 legend(order(1 "High Saving" 2 "Low Saving")) xtitle("HH Adjusted Disposable Income") ytitle("Density")
graph export "$output/fig_b5.pdf", replace
restore

*************
* Figure B6
*************

preserve
b
qui sum totaldebt
gen meanD =  r(mean)

gen lowdebt = 1 if totaldebt >=0 & totaldebt <=meanD
replace lowdebt = 0 if totaldebt >meanD & totaldebt !=.

twoway (kdensity z_income_disHH if lowdebt == 0 , color(orange)  ) ///
 (kdensity z_income_disHH if lowdebt == 1,  lpattern(solid) color(black) ), ///
 legend(order(1 "High Debt" 2 "Low Debt")) xtitle("HH Adjusted Disposable Income") ytitle("Density")
graph export "$output/fig_b6.pdf", replace

restore


*************
* Figure D5
*************

preserve 
b
do "$code/9-figd5"
restore

preserve
b

gen nochangeECON = 1 if geneconexp1 ==  "Stay the same" | geneconexp1 == "stayed the same" 
replace nochangeECON = 0 if geneconexp1 == "Get a little better" | geneconexp1 == "Get a little worse" | ///
geneconexp1 == "Get a lot better" | geneconexp1 == "Get a lot worse" | geneconexp1 == "get a little better" | /// 
geneconexp1 == "get a little worse" | geneconexp1 == "get a lot worse" | | geneconexp1 == "get a lot better"


gen nochangePERSONAL = 1 if hhfinexp1 ==  "Stay the same" | hhfinexp1 == "stayed the same" 
replace nochangePERSONAL = 0 if hhfinexp1 == "Get a little better" | hhfinexp1 == "Get a little worse" | ///
hhfinexp1 == "Get a lot better" | hhfinexp1 == "Get a lot worse" | hhfinexp1 == "get a little better" | /// 
hhfinexp1 == "get a little worse" | hhfinexp1 == "get a lot worse" | | hhfinexp1 == "get a lot better"

qui reg nochangeECON z_p_wealth z_income_disHH $cfef ,r
qui margins, at(z_p_wealth = (-1 0 1 2)) post
regsave  using "$temp/fig5data", replace   ci

qui reg nochangePERSONAL z_p_wealth z_income_disHH  $cfef ,r
qui margins, at(z_p_wealth = (-1 0 1 2)) post
regsave  using "$temp/fig5data", append   ci
restore


************************************************************************
* 							5. Data Analysis
************************************************************************

do "$code/4-tab2" // Produces Table 2
do "$code/10-fige1" // Produces Figure E1
do "$code/11-fige2" // Produces Figure E2

do "$code/5-tabd1" // Produces Table D1
do "$code/6-tabd2" // Produces Table D2
do "$code/7-figd4" // Produces Figure D4


b
do "$code/8-tab3" // Produces Table 3
do "$code/9-figd5" // Produces Figure D5




log close





